import java.sql.*;
import java.util.ArrayList;

import controlP5.Bang;
import controlP5.ControlCanvas;
import controlP5.ControlGroup;
import controlP5.ControlP5;
import controlP5.ScrollList;
import processing.core.PApplet;
import processing.core.PFont;

public class Main extends PApplet {
	//static parameters
	public final static int WIDTH = 400;
	public final static int HEIGHT = 800;	
	public final static int FRAME_RATE = 30;
	
	//public variables to access Processing and ControlP5 libraries
	public static ControlP5 controlP5;
	public PApplet theApplet = this;
	public CSVConn myconn;
	
	//static variables 
	public static ArrayList<MainGroup> groupList;  //holds all created groups
	public static MainGroup main;				   //group "main" to hold all objects that do not fall in another group
	public static DrawObject activeObject;		   //object selected by user, when mouse button is not down it takes null value
	
	
	int testCanvas_xPos = 100;
	int testCanvas_yPos = 200;
	int testCanvas_width = 300;
	int testCanvas_height = 400;
	
	public ArrayList<ArrayList<Float>> myList; 
	
	
	//sets up window and p5 start parameters
	public void setup() {
		size(WIDTH,HEIGHT);
		smooth();
		frameRate(FRAME_RATE);
		
		controlP5 = new ControlP5(this);
		
		initializeData();
		initializeGroups();

	}
	
	//draws objects at each iteration of the buffer
	//should not need to use at this level
	public void draw() {
		 background(0);	
		 //drawStuff();
	}
	void drawStuff() {
		rect(0,0,50,50);
	}
	
	//handles mouse press event
	//checks all open groups to see if mouse click is in their range
	//goes in order specified by groupList, then checks "main" group
	public void mousePressed() {
		for(MainGroup g: groupList) {
			if(g.handlePress(mouseX, mouseY)) {
				return;
			}
			main.handlePress(mouseX, mouseY);
		}
	}
	
	//handles mouse release event
	//calls handleRelease function in DrawObject class
	public void mouseReleased() {
		if(activeObject != null) {
			activeObject.handleRelease(mouseX, mouseY);
		}
	}
	
	
	//Initialize a couple groups for testing purposes
	void initializeGroups() {
		groupList = new ArrayList<MainGroup>();
		main = new MainGroup( "MainGroup", this, controlP5,0, 0, width, height);
		main.theGroup.setBackgroundColor(color(0,0,0,255));
				
		for(int i = 0; i< 1; i++) {
			MainGroup g = new MainGroup( "Group: "+i, this, controlP5,testCanvas_xPos, testCanvas_yPos+ 50*i, testCanvas_width, testCanvas_height);
			groupList.add(g);
			DrawObject c = new DrawObject(this, g.theCanvas, g.theGroup, 100, 100);
			g.theCanvas.drawQueue.add(c);
			g.theCanvas.parent.theGroup.addCanvas(c);
		}		
		
		int i= groupList.size()+1;
		MainGroup g = new MainGroup( "Group: "+ i, this, controlP5,testCanvas_xPos, testCanvas_yPos+ 50*i, testCanvas_width, testCanvas_height);
		groupList.add(g);
		DrawObject c = new RasterTest(this, g.theCanvas, g.theGroup, 100, 100, myList);
		g.theCanvas.drawQueue.add(c);
		g.theCanvas.parent.theGroup.addCanvas(c);
		
	}
	
	//Initialize data
	void initializeData() {
		myconn = new CSVConn("../Data/");	
		
		// just load everything
		myList = new ArrayList< ArrayList<Float> >();
		myList.ensureCapacity(4329);
		
		if(myconn.query("SELECT * FROM partial_genes4329") == 1) {
			ResultSet results = myconn.getResults();
			try
			{
				int row = 0;
				results.next();
				while (results.next())
				{	
					myList.add( new  ArrayList<Float> () );				
					for(int col = 1; col < 13; col+=1) 
					{	
						(myList.get(row)).add(col-1, results.getFloat(col+1));
					}
					row+=1;
				}
			}
			catch(Exception e)
			{
				System.out.println("Oops-> " + e);
			}
		}
		
//		
//		for(int row = 0; row < 4328; row+=1) { 
//			for(int col = 0; col < 12; col+=1) {
//				print(myList.get(row).get(col) + " ");
//			}
//			println("\n");
//		}
		
		
	}

}
